﻿using System;
using System.ComponentModel.Composition;
using System.Web.Mvc;
using PAMD.Business.Models;
using PAMD.Controllers.Helper;
using PAMD.Database.Services;
using System.Web.Security;
using PAMD.Controllers.Extension;

namespace PAMD.Controllers.Account
{
    [Export]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    [Authorize]
    public class ProfileController : Controller
    {
        [Import(typeof(ProfileService))]
        private ProfileService ProfileService { get; set; }

        [Import(typeof(CommonService))]
        private CommonService CommonService { get; set; }

        [Import(typeof(AccountService))]
        private AccountService AccountService { get; set; }

        public ActionResult Index()
        {
            string username = ((FormsIdentity)User.Identity).Ticket.UserData;
            ProfileModel model = new ProfileModel();

            model.UserDetail = ProfileService.GetByUserName(username);
            if (model.UserDetail.AddressID == 0)
            {
                model.Address = null;
            }
            else
            {
                model.Address = ProfileService.GetAddreess(model.UserDetail.AddressID);
            }

            EnsureIdToTexts(model);

            return View(model);
        }

        public ActionResult Edit(ProfileModel model)
        {
            string username = ((FormsIdentity)User.Identity).Ticket.UserData;
            model.UserDetail = ProfileService.GetByUserName(username);
            if (model.UserDetail.AddressID == 0)
            {
                model.Address = null;
            }
            else
            {
                model.Address = ProfileService.GetAddreess(model.UserDetail.AddressID);
            }

            EnsureIdToTexts(model);
            return View(model);
        }

        public ActionResult Save(ProfileModel model)
        {
            string username = ((FormsIdentity)User.Identity).Ticket.UserData;
            var userDetail = model.UserDetail;
            var address = model.Address;
            userDetail.Email = username;
            bool isSaved = ProfileService.UpdateUser(userDetail, address);
            if (isSaved)
            {
                model.UserDetail = ProfileService.GetByUserName(username);
                if (model.UserDetail.AddressID== 0)
                {
                    model.Address = null;
                }
                else
                {
                    model.Address = ProfileService.GetAddreess(model.UserDetail.AddressID);
                }

                EnsureIdToTexts(model);
                ViewBag.Error = "You profile updated successfully.";
                return View("Index", model);
            }

            EnsureIdToTexts(model);
            ViewBag.Error = "You profile could not saved successfully.";
            return View("Edit", model);
        }

        public ActionResult ChangePassword(ResetPasswordModel model)
        {
            if (model.CurretPassword != null)
            {
                string email = ((FormsIdentity)User.Identity).Ticket.UserData;
                var userDetails = AccountService.GetByUserName(email);
                if (userDetails != null && userDetails.Password == model.CurretPassword)
                {
                    AccountService.ChangePassword(email, model.Password);
                    ViewBag.Message = "You profile updated successfully.";
                    return View(model);
                }
                ViewBag.Error = "You profile could not saved successfully. Please enter correct details";
            }            
            return View(model);
        }

        private void EnsureIdToTexts(ProfileModel model)
        {
            model.Countries = CommonService.GetCountryList().ToSelectListItems();
            model.OccupationList = CommonService.GetOccupationList().ToSelectListItems();

            if (!string.IsNullOrEmpty(model.UserDetail.Country))
            {
                model.CountryText = model.Countries.Find(c => c.Value == model.UserDetail.Country).Text;
            }
            if (!string.IsNullOrEmpty(model.UserDetail.Occupation))
            {
                model.OccupationText = model.OccupationList.Find(c => c.Value == model.UserDetail.Occupation).Text;
            }
        }
    }
}